package com.googlecode.bluetools.dom4j;

import org.xml.sax.Locator;

public class Location implements Comparable<Location> {

	public int line;
	public int column;

	public Location(int line, int column) {
		this.line = line;
		this.column = column;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + column;
		result = prime * result + line;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Location other = (Location) obj;
		if (column != other.column)
			return false;
		if (line != other.line)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Location {" + line + ", " + column + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	public static Location newInstance(Locator locator) {
		return new Location(locator.getLineNumber(), locator.getColumnNumber());
	}

	public int compareTo(Location another) {
		if (line < another.line)
			return -1;
		else if (line == another.line) {
			if (column < another.column)
				return -1;
			else if (column == another.column)
				return 0;
			else
				return 1;
		}
		else
			return 1;
	}
}
